#include "CCmdInnerActTitle.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_ACT_TITLE, CCmdInnerActTitle);

int CCmdInnerActTitle::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_WARNING(("inner cmd CCmdInnerActTitle get data"));
        return Done(RET_OK);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return Done(RET_SYSTEM_FAIL);
    }

    if (_uiGID == 0 && _auiAllGID.Size() <= 0)
    {
        CT_WARNING((""));
        return Done(RET_OK);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdInnerActTitle::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        return Done(iRet);
    }

	switch (_iSubCmd)
	{
    //下发全量活动列表 等待下一次命令刷出或者显式立即刷出
    case CActMgr::CMD_ACT_OPT_SUB_NOTIFY_ACT_LIST:
        {
            PREPARE_PLAYER_BASE_INFO(this, _uiGID);
            ASSERT_PLAYER_PREPARED(_uiGID);
            
            PREPARE_PLAYER_DETAIL_INFO(this, _uiGID);
            ASSERT_PLAYER_DETAIL_PREPARED(_uiGID);
            
            int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no base data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerBaseInfo& stPlayerBase =
                pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];
            
            if (stPlayerBase._ucIsInited != 1)
            {
                return Done(ERROR_NOT_INITED);
            }
            
            iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no detail data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerDetailInfo& stPlayerDetail =
                pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

            MsgTips stTips;
            pstMainCtrl->m_stActMgr.GetActList(stPlayerBase, stPlayerDetail,
                stTips);
            pstMainCtrl->NotifyOnlineUser(_uiGID, ID_MAIN_TIPS, stTips, true);
            return Done(RET_OK);//命令不需要响应包直接Done
        }
        break;

    //下发全量活动列表 等待下一次命令刷出或者显式立即刷出
    case CActMgr::CMD_ACT_OPT_SUB_NOTIFY_ACT_LIST_TO_ALL:
        {
            if (_iIndex < 0) {_iIndex = 0;}
            for ( ; _iIndex < _auiAllGID.Size(); ++_iIndex)
            {
                _uiGID = _auiAllGID[_iIndex];

                PREPARE_PLAYER_BASE_INFO(this, _uiGID);
                ASSERT_PLAYER_PREPARED(_uiGID);
                
                PREPARE_PLAYER_DETAIL_INFO(this, _uiGID);
                ASSERT_PLAYER_DETAIL_PREPARED(_uiGID);

                CT_TRACE(("Notify Act List To All(time=%d role=%u)!",
                    pstMainCtrl->m_iNow,_uiGID));
                
                int iIndex =
                        pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(_uiGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no base data user(%u)", _uiGID));
                    continue;
                }
                CPlayerBaseInfo& stPlayerBase =
                    pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];
                
                if (stPlayerBase._ucIsInited != 1)
                {
                    continue;
                }
                
                iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(_uiGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail data user(%u)", _uiGID));
                    continue;
                }
                CPlayerDetailInfo& stPlayerDetail =
                    pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

                MsgTips stTips;
                pstMainCtrl->m_stActMgr.GetActList(stPlayerBase, stPlayerDetail,
                    stTips);

                pstMainCtrl->NotifyOnlineUser(_uiGID, ID_MAIN_TIPS, stTips,
                    true);
            }
        }
        break;

    //准备数据并单独下发MsgAnsMapInfo中的状态列表和冥想状态
    case CActMgr::CMD_ACT_OPT_SUB_NOTIFY_STATUS_TO_SELF:
        {
            PREPARE_PLAYER_BASE_INFO(this, _uiGID);
            ASSERT_PLAYER_PREPARED(_uiGID);
            
            PREPARE_PLAYER_DETAIL_INFO(this, _uiGID);
            ASSERT_PLAYER_DETAIL_PREPARED(_uiGID);
            
            PREPARE_PLAYER_SNS_DETAIL_INFO(this,_uiGID);
            ASSERT_PLAYER_SNS_DETAIL_PREPARED(_uiGID);
            
            PREPARE_PLAYER_ONLINE_DATA(this, _uiGID);
            ASSERT_PLAYER_ONLINE_DATA_PREPARED(_uiGID);

            int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no base data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerBaseInfo& stPlayerBase =
                pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];
            
            if (stPlayerBase._ucIsInited != 1)
            {
                return Done(ERROR_NOT_INITED);
            }
            
            iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no detail data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerDetailInfo& stPlayerDetail =
                pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];
            iIndex = pstMainCtrl->FindPlayerCacheData(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no cache data user %u", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerCacheData& stPlayerCache =
                pstMainCtrl->m_stPlayerCacheDataHash[iIndex];
            
            iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no online data user %u", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerOnlineData& stPlayerOnline =
                pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];
            
            iIndex = pstMainCtrl->m_stPlayerSnsCommInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no sns comm data user %u", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerSnsCommInfo& stPlayerSnsCommInfo =
                pstMainCtrl->m_stPlayerSnsCommInfoHash[iIndex];

            pstMainCtrl->m_stStatusMgr.NotifyUserStatusToSelf(stPlayerBase,
                stPlayerDetail, stPlayerOnline, stPlayerCache,
                stPlayerSnsCommInfo);
            return Done(RET_OK);//命令不需要响应包直接Done
        }
        break;

    //准备数据并单独下发MsgAnsMapInfo中的状态列表和冥想状态
    case CActMgr::CMD_ACT_OPT_SUB_NOTIFY_STATUS_TO_MAP:
        {
            PREPARE_PLAYER_BASE_INFO(this, _uiGID);
            ASSERT_PLAYER_PREPARED(_uiGID);
            
            PREPARE_PLAYER_DETAIL_INFO(this, _uiGID);
            ASSERT_PLAYER_DETAIL_PREPARED(_uiGID);
            
            PREPARE_PLAYER_SNS_DETAIL_INFO(this,_uiGID);
            ASSERT_PLAYER_SNS_DETAIL_PREPARED(_uiGID);
            
            PREPARE_PLAYER_ONLINE_DATA(this, _uiGID);
            ASSERT_PLAYER_ONLINE_DATA_PREPARED(_uiGID);

            int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no base data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerBaseInfo& stPlayerBase =
                pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];
            
            if (stPlayerBase._ucIsInited != 1)
            {
                return Done(ERROR_NOT_INITED);
            }
            
            iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no detail data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerDetailInfo& stPlayerDetail =
                pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];
            iIndex = pstMainCtrl->FindPlayerCacheData(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no cache data user %u", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerCacheData& stPlayerCache =
                pstMainCtrl->m_stPlayerCacheDataHash[iIndex];
            
            iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no online data user %u", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerOnlineData& stPlayerOnline =
                pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];
            
            iIndex = pstMainCtrl->m_stPlayerSnsCommInfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no sns comm data user %u", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerSnsCommInfo& stPlayerSnsCommInfo =
                pstMainCtrl->m_stPlayerSnsCommInfoHash[iIndex];

            pstMainCtrl->m_stStatusMgr.NotifyUserStatusToMap(stPlayerBase,
                stPlayerDetail, stPlayerOnline, stPlayerCache,
                stPlayerSnsCommInfo);
            return Done(RET_OK);//命令不需要响应包直接Done
        }
        break;

    //准备数据并更新奖励找回 玩家参加活动
    case CActMgr::CMD_ACT_OPT_SUB_REWARD_BACK_SET_ACT:
        {
            PREPARE_PLAYER_DETAIL_EX1_INFO(this, _uiGID);
            ASSERT_PLAYER_DETAIL_EX1_INFO(_uiGID);
            
            int iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(
                    _uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no detail ex1 data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerDetailInfoEx1& stPlayerDetailEx1 =
                pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

            pstMainCtrl->m_stRewardBackMgr.SetPlayerAct(stPlayerDetailEx1,
                _iActID);
            return Done(RET_OK);//命令不需要响应包直接Done
        }
        break;

    //准备数据并更新登录奖励 下发在线玩家
    case CActMgr::CMD_ACT_OPT_SUB_NOTIFY_LOGIN_REWARD:
        {
            if (_iIndex < 0) {_iIndex = 0;}
            for ( ; _iIndex < _auiAllGID.Size(); ++_iIndex)
            {
                _uiGID = _auiAllGID[_iIndex];

                PREPARE_PLAYER_BASE_INFO(this, _uiGID);
                ASSERT_PLAYER_PREPARED(_uiGID);
                
                PREPARE_PLAYER_DETAIL_EX1_INFO(this, _uiGID);
                ASSERT_PLAYER_DETAIL_EX1_INFO(_uiGID);

                int iIndex =
                        pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(_uiGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no base data user(%u)", _uiGID));
                    continue;
                }
                CPlayerBaseInfo& stPlayerBase =
                    pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];
                
                if (stPlayerBase._ucIsInited != 1)
                {
                    continue;
                }
                
                iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(
                        _uiGID);
                if (iIndex < 0)
                {
                    CT_ERROR(("no detail ex1 data user(%u)", _uiGID));
                    return Done(RET_SYSTEM_FAIL);
                }
                CPlayerDetailInfoEx1& stPlayerDetailEx1 =
                    pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

                bool bUpdateToDB = false;
                pstMainCtrl->m_stLoginRewardMgr.UpdateData(stPlayerDetailEx1,
                    bUpdateToDB);
                if (bUpdateToDB)
                {
                    MsgAnsLoginReward stLoginRewardAns;
                    stLoginRewardAns.set_gid(_uiGID);
                    stLoginRewardAns.set_req_type(
                        CLoginRewardMgr::CMD_LOGIN_REWARD_SUB_INFO);
                    pstMainCtrl->m_stLoginRewardMgr.GetInfo(stPlayerDetailEx1,
                        stLoginRewardAns);
                    pstMainCtrl->NotifyOnlineUser(_uiGID, ID_MAIN_LOGIN_REWARD,
                        stLoginRewardAns, true);
                }
            }
        }
        break;

    //准备数据并更新领红包数据
    case CActMgr::CMD_ACT_OPT_SUB_RED_PACKET_UPDATE:
        {
            PREPARE_PLAYER_DETAIL_EX1_INFO(this, _uiGID);
            ASSERT_PLAYER_DETAIL_EX1_INFO(_uiGID);
            
            int iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(_uiGID);
            if (iIndex < 0)
            {
                CT_ERROR(("no detail ex1 data user(%u)", _uiGID));
                return Done(RET_SYSTEM_FAIL);
            }
            CPlayerDetailInfoEx1& stPlayerDetailEx1 =
                pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

            if (_iIndex >= 0 && _iIndex < stPlayerDetailEx1._stSplendidActEx1._astRedPacket.Size())
            {
                if (_iAuxData1 > 0)
                {
                    stPlayerDetailEx1._stSplendidActEx1._astRedPacket[_iIndex]._iGold += _iAuxData1;
                }
                if (_iAuxData2 > 0)
                {
                    stPlayerDetailEx1._stSplendidActEx1._astRedPacket[_iIndex]._iSilver += _iAuxData2;
                }
                if (_iAuxData1 > 0 || _iAuxData2 > 0)
                {
                    pstMainCtrl->m_stRewardBackMgr.UpdateDetailEx1ToDB(stPlayerDetailEx1);
                }
            }
            else
            {
                CT_ERROR(("user(%u) gold(%d) silver(%d) index(%d) error", _uiGID, _iAuxData1, _iAuxData2, _iIndex));
            }

            return Done(RET_OK);//命令不需要响应包直接Done
        }
        break;

	default:
		iRet = ERROR_MAIN_SUB_CMD;
		break;
	}

    if (iRet)
    {
        CT_ERROR(("user(gid=%u) deal act title op(sub=%d) error(%d)",
            _uiGID, _iSubCmd, iRet));
        return Done(iRet);
    }

    return Done(RET_OK);
}

